import net.aetherial.context.group.*;

class net.aetherial.context.group.MemberView extends MovieClip
{
	public var memberVar:Member;
	public var startDate:Date;
	public var endDate:Date;
	
	public var hourWidth:Number;
	
	public var background:MovieClip;
	public var icon:MovieClip;
	public var clips:Array;
	public var username:TextField;
	

	public function MemberView ()
	{
		this.clips = new Array ();
	}
	
	public function set member (newMember:Member)
	{
		/* this.onRollOver = function ()
		{
			GroupView.showStatus (this.memberVar.name, _parent._xmouse, _parent._ymouse);
		}

		this.onRollOut = function ()
		{
			GroupView.showStatus ("", 0, 0);
		} */

		this.memberVar = newMember;

		this.onEnterFrame = function ()
		{
			this.background._height = 60.0;
			this.background._width = Stage.width;

			var updates:Array = new Array ();

			while (clips.length > 0)
			{
				var clip:MovieClip = clips.pop ();
				clip.removeMovieClip ();
			}

			for (var i = 0; i < memberVar.updates.length; i++)
			{
				var update = memberVar.updates[i];
			
				if (update.date > this.startDate)
					updates.push (update);
			}
			
			var lastDates:Object = new Object ();
			
			for (var i = 0; i < updates.length; i++)
			{
				var update:MovieClip = updates[i];
				
				var offset:Number = -1;
				
				if (update.field == "Activity")
					offset = 0;
				else if (update.field == "Location")
					offset = 1;
				else if (update.field == "Social Context")
					offset = 2;
					
				if (offset >= 0)
				{
					var clip:MovieClip = this.attachMovie ("LabelBar", "lb_" + i, this.getNextHighestDepth());
					clips.push (clip);
				 
					var x = ((update.date.getTime () - this.startDate.getTime ()) / (3600 * 1000)) * hourWidth;
				
					clip._x = Math.round (x); 
					clip._y = Math.round (offset * clip._height);
					
					var found:Boolean = false;
					
					for (var j = i + 1; j < updates.length; j++)
					{
						if (updates[j].field == update.field)
						{
							found = true;
							
							var duration = (updates[j].date.getTime () - update.date.getTime ()) / (3600 * 1000);
							
							clip.tail._width = Math.round (duration * hourWidth) + 1;
						}
						
						if (found)
							break;
					}
					
					if (!found)
						clip.tail._width = Math.round (3 * hourWidth) + 1;

					clip.text.autoSize = "left";
					clip.text.text = update.value;
					
					if (clip.text._width < clip.tail._width)
						clip.text._visible = true;
					else
						clip.text._visible = false;

						
					clip.name = newMember.name;
					clip.field = update.field;
					clip.value = update.value;
					
					clip.date = "";
					
					if (update.date.getHours () < 10)
						clip.date += "0"
					
					clip.date += update.date.getHours () + ":";
					
					if (update.date.getMinutes () < 10)
						clip.date += "0"

					clip.date += update.date.getMinutes ();
					
					clip.onRollOver = function ()
					{
						this.tail.gotoAndStop ("active");
						this.cap.gotoAndStop ("active");
						
						GroupView.showStatus (this.name, this.field, this.value, this.date);
					}

					clip.onRollOut = function ()
					{
						GroupView.hideStatus ();

						this.tail.gotoAndStop ("inactive");
						this.cap.gotoAndStop ("inactive");
					}

				}
			}
			
			username.removeTextField ();
			
			username = this.createTextField ("username", this.getNextHighestDepth(), 0, 0, 16, 50); 
			username.multiline = false; 
			username.wordWrap = false;
			username.autoSize = "left";
			username._y = background._height;

			var my_fmt:TextFormat = new TextFormat(); 
			my_fmt.color = 0xCCCCCC; 
			my_fmt.font = "Verdana";
			my_fmt.size = 18;
			
		
			username.text = memberVar.name; 
			username.setTextFormat (my_fmt); 

			icon.removeMovieClip ();
			
			icon = this.createEmptyMovieClip ("icon", this.getNextHighestDepth ()); 
			icon._x = 0;
			icon._y = 0;

			var mcl:MovieClipLoader = new MovieClipLoader (); 
			
			var listener:Object = new Object ();
			listener.onLoadInit = function (mc:MovieClip) 
			{ 

			} 
			
			listener.onLoadComplete = function (target:MovieClip, httpStatus:Number)
			{
				target.onEnterFrame = function ()
				{
					this._x = -60.0;
					this._width = 60.0;
					this._height = 60.0;
					
					this.onEnterFrame = undefined;
				}
			}
			
			mcl.addListener (listener);
			mcl.loadClip(memberVar.iconUrl, icon); 
			
			this.onEnterFrame = undefined;
		}
	}
	
	public function onEnterFrame ()
	{
		this.background._height = 30.0;
		this.background._width = Stage.width;
		
		this.onEnterFrame = undefined;
	}
}